package cn.wolfcode.web.controller;


import cn.wolfcode.transfer.domain.Employee;
import cn.wolfcode.transfer.domain.LoginNotice;
import cn.wolfcode.transfer.domain.Notice;
import cn.wolfcode.transfer.qo.JsonResult;
import cn.wolfcode.transfer.qo.NoticeQueryObject;
import cn.wolfcode.transfer.service.INoticeService;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.util.RequiredPermission;
import cn.wolfcode.transfer.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiredPermission(name="公告页面",expression="notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){

        Employee currentUser = UserContext.getCurrentUser();
        Boolean admin = currentUser.getAdmin();

        model.addAttribute("admin",admin);
        model.addAttribute("qo",qo);
        if (admin){
            PageInfo<Notice> pageInfo = noticeService.listAll(qo);
            model.addAttribute("pageInfo", pageInfo);
        }else {
            PageInfo<Notice> pageInfo = noticeService.query(qo);
            model.addAttribute("pageInfo", pageInfo);
        }

        return "/notice/list";
    }


    @RequiredPermission(name="公告删除",expression="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.get(id));
        }
        return "/notice/show";
    }


    @RequiredPermission(name="公告查看",expression="notice:show")
    @RequestMapping("/show")
    public String show(Long id, Model model) {

            model.addAttribute("notice",noticeService.see(id));
        return "/notice/show";
    }
    @RequiredPermission(name="公告取消发布",expression="notice:cancel")
    @RequestMapping("/cancel")
    public String cancel(Long id, Model model) {

           noticeService.cancel(id);
        return "redirect:/notice/list";
    }

    @RequiredPermission(name="公告发布",expression="notice:push")
    @RequestMapping("/push")
    @ResponseBody
    public JsonResult push(Long id) {
        Notice notice = noticeService.get(id);
        if (notice.isPublishStatus()) {
            noticeService.releaseAgain(id);
        }else {
            noticeService.push(id);
        }
        return new JsonResult();
    }

    @RequestMapping("/refreshNotice")
    @ResponseBody
    public JsonResult refreshNotice() {
        LoginNotice loginNotice = noticeService.loginByIdGetNotice(UserContext.getCurrentUser().getId());
        System.out.println(loginNotice.getSize());
        System.out.println(loginNotice.getNotices() != null);

        if (loginNotice.getNotices()!=null&&loginNotice.getSize()!=0){
            UserContext.setNotice(loginNotice);
        }
        return new JsonResult();
    }
}
